package mapper;

import Conn.Conn;
import com.alibaba.fastjson.JSONObject;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class Findname {
    /*
    类: Findname
    方法: NameList
    方法说明:
    index 所使用
        传入 String类型 进行数据库查询
        并将查询结果保存成列表存入 Json 的 List 键下
     */
    private static PreparedStatement pstam;
    public JSONObject NameList(String name) {
        Conn conn = new Conn();
        JSONObject obj = new JSONObject();
        try {
            pstam = conn.getConn().prepareStatement("select text from test where name = ? and design = 0");
            pstam.setString(1,name);
            ResultSet resultSet = pstam.executeQuery();
            List<String> list = new ArrayList<>();
            while (resultSet.next()){
//                System.out.println(resultSet.getString("text"));
                list.add(resultSet.getString("text"));
            }

            if(list.toArray().length == 0){
                list.add("暂无内容");
            }

            obj.put("List",list);
            return obj;


        }catch (Exception e){
            e.printStackTrace();
            return null;

        }
    }

}
